{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Chapter 08\n",
    "# 增加矩阵形状不匹配的报错信息\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "# 自定义函数\n",
    "def matrix_multiplication(A, B):\n",
    "    # 检查两个矩阵形状是否匹配\n",
    "    if len(A[0]) != len(B):\n",
    "        raise ValueError(\"Error: check matrix sizes\")\n",
    "    else:\n",
    "        # 定义全 0 矩阵 C 用来存放结果\n",
    "        C = [[0] * len(B[0]) for i in range(len(A))]\n",
    "        # 遍历 A 的行\n",
    "        for i in range(len(A)):  # len(A) 给出 A 的行数\n",
    "            # 遍历 B 的列\n",
    "            for j in range(len(B[0])):\n",
    "                # len(B[0]) 给出 B 的列数\n",
    "                # 这一层相当于消去 p 所在的维度，即压缩\n",
    "                for k in range(len(B)):\n",
    "                    C[i][j] += A[i][k] * B[k][j]\n",
    "                    # 完成对应元素相乘，再求和\n",
    "\n",
    "        return C"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:03:24.585622Z",
     "end_time": "2024-07-03T16:03:24.589218Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A @ B = \n",
      "[1, 2, 3]\n",
      "[2, 4, 6]\n",
      "[3, 6, 9]\n",
      "[4, 8, 12]\n"
     ]
    }
   ],
   "source": [
    "# 定义矩阵 A 和 B\n",
    "A = [[1], [2], [3], [4]]\n",
    "B = [[1, 2, 3]]\n",
    "print('A @ B = ')\n",
    "C = matrix_multiplication(A, B)  # 调用自定义函数\n",
    "for row in C:\n",
    "    print(row)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:03:39.210182Z",
     "end_time": "2024-07-03T16:03:39.219559Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "B @ A = \n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "Error: check matrix sizes",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[3], line 2\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mB @ A = \u001B[39m\u001B[38;5;124m'\u001B[39m)\n\u001B[1;32m----> 2\u001B[0m D \u001B[38;5;241m=\u001B[39m \u001B[43mmatrix_multiplication\u001B[49m\u001B[43m(\u001B[49m\u001B[43mB\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mA\u001B[49m\u001B[43m)\u001B[49m  \u001B[38;5;66;03m# 会报错\u001B[39;00m\n",
      "Cell \u001B[1;32mIn[1], line 5\u001B[0m, in \u001B[0;36mmatrix_multiplication\u001B[1;34m(A, B)\u001B[0m\n\u001B[0;32m      2\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mmatrix_multiplication\u001B[39m(A, B):\n\u001B[0;32m      3\u001B[0m     \u001B[38;5;66;03m# 检查两个矩阵形状是否匹配\u001B[39;00m\n\u001B[0;32m      4\u001B[0m     \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mlen\u001B[39m(A[\u001B[38;5;241m0\u001B[39m]) \u001B[38;5;241m!=\u001B[39m \u001B[38;5;28mlen\u001B[39m(B):\n\u001B[1;32m----> 5\u001B[0m         \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mError: check matrix sizes\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n\u001B[0;32m      6\u001B[0m     \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[0;32m      7\u001B[0m         \u001B[38;5;66;03m# 定义全 0 矩阵 C 用来存放结果\u001B[39;00m\n\u001B[0;32m      8\u001B[0m         C \u001B[38;5;241m=\u001B[39m [[\u001B[38;5;241m0\u001B[39m] \u001B[38;5;241m*\u001B[39m \u001B[38;5;28mlen\u001B[39m(B[\u001B[38;5;241m0\u001B[39m]) \u001B[38;5;28;01mfor\u001B[39;00m i \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mrange\u001B[39m(\u001B[38;5;28mlen\u001B[39m(A))]\n",
      "\u001B[1;31mValueError\u001B[0m: Error: check matrix sizes"
     ]
    }
   ],
   "source": [
    "print('B @ A = ')\n",
    "D = matrix_multiplication(B, A)  # 会报错"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
